﻿/****************************************************************************
*  Arthea Server by R. Jennings (2007-2008)   http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses.      *
****************************************************************************/

using System.Collections.Generic;
using System.Xml.Serialization;

namespace Arthea.Abilities.Spells
{

    /// <summary>
    /// Implementation of <see cref="SpellList"/>
    /// </summary>
    public class SpellList : IXmlSerializable
    {
        private List<Spell> spells = new List<Spell>();

        /// <summary>
        /// Adds the specified spell.
        /// </summary>
        /// <param name="spell">The spell.</param>
        public void Add(Spell spell)
        {
            spells.Add(spell);
        }

        /// <summary>
        /// Removes the specified spell.
        /// </summary>
        /// <param name="spell">The spell.</param>
        /// <returns></returns>
        public bool Remove(Spell spell)
        {
            return spells.Remove(spell);
        }

        #region IXmlSerializable Members

        /// <summary>
        /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
        /// </returns>
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
        public void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.IsEmptyElement)
                return;

            reader.ReadStartElement();

            while (reader.IsStartElement())
            {
                Spell sp = AbilityList.FindSpell(reader.ReadElementContentAsString());
                if (sp != null)
                    spells.Add(sp);
            }

            reader.ReadEndElement();
        }

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (spells.Count == 0)
                return;

            foreach (Spell sp in spells)
                writer.WriteElementString("Spell", sp.Name);
        }

        #endregion
    }
}
